<?php
namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\Response;
use Illuminate\Http\Request;
use App\User;
use Illuminate\Support\Facades\Auth;
class IndexController extends BaseController
{


    /**
     * Show the application dashboard to the user.
     *
     * @return Response
     */
    public function index()
    {
        if(!$this->user->can(array('employ','daskbord','teaches','bbs','user','setting','content','server'))) abort(405);
        
        $data = array();
        $data['os'] = php_uname('s');
        $data['method'] = php_sapi_name();
        $data['phpversion'] = PHP_VERSION;
        $data['serverName'] = $_SERVER['HTTP_HOST'];
        $data['port'] = $_SERVER['SERVER_PORT'];
        $data['web'] = $_SERVER['SERVER_SOFTWARE'];
//         $data['user'] = $_SERVER["USER"];
        $data['http'] = $_SERVER["SERVER_PROTOCOL"];
        $data['httpstatus'] = $_SERVER["HTTP_CONNECTION"];
        $data['upload_enable'] = ini_get('file_uploads');
        $data['file_max_size'] = ini_get('post_max_size');
        $data['charset'] = ini_get('default_charset');
        return view('admin.index')->with('data',$data);
    }

    /**
     * 个人信息
     */
    public function owner()
    {
        if(!$this->user->can('viewOwnerInfo')) abort(405);
        
        return view('admin.owner');
    }

    /**
     * 个人信息
     */
    public function ownerSave(Request $req)
    {
        if(!$this->user->can('editOwnerInfo')) abort(405);
        
        $data = $req->only('uid','name','email','phone','qq');
        $data['uid'] = $data['uid']?:Auth::user()->id;
        $userInfo = User::find($data['uid']);
        if(count($userInfo) > 0 )
        {
            $userInfo->name = trim($data['name']);
            $userInfo->email = trim($data['email']);
            $userInfo->phone = $data['phone'];
            $userInfo->qq = $data['qq'];
            $userInfo->save();
        }
        else
        {
            abort(403);
        }
        return redirect()->intended('admin/index/owner');
    }

    /**
     * 修改密码
     */
    public function  modifyPwd(Request $req)
    {
        if(!$this->user->can('editOwnerInfo')) abort(405);
        
        $data = $req->only('oldpwd','newpwd','newpwd_repeat','uid');
        if(($data['uid'] == Auth::user()->id) && $data['uid'] > 0)
        {
            $userPwd = User::find($data['uid']);
            if($userPwd->password !== $data['oldpwd'])
            {
                return redirect()->intended('admin/index/modifypwd')->withErrors(['oldpassword'=>'原密码输入错误']);
            }
            if($data['newpwd'] !== $data['newpwd_repeat'])
            {
                return redirect()->intended('admin/index/modifypwd')->withErrors(['newpassword'=>'两次密码输入不一致，请检查后重新输入']);
            }
            $userPwd->password = $data['newpwd'];
            $userPwd->save();
            return redirect()->intended('admin/index/modifypwd');
        }
        else
        {
            return view('admin.pwd');
        }
    }
}

